package com.itheima.service.impl;

import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Encounter
 * @date 2024/10/10 12:49<br/>
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService
    {
        @Autowired
        private OrderMapper orderMapper;
        @Autowired
        private CheckGroupServiceImpl checkGroupService;
        
        /**
         * 动态分页查询
         *
         * @param queryPageBean 查询页面 Bean
         */
        @Override
        public PageResult queryPage(QueryPageBean queryPageBean)
            {
                //解析查询条件
                String queryString = queryPageBean.getQueryString();
                Map<String, Object> queryMap = checkGroupService.parseQueryConditions(queryString);
                log.info("queryMap:{}", queryMap);
                
                //设置分页参数
                PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
                //开始查询
                Page<OrderVO> orderVOPage = orderMapper.query(queryMap);
                
                //封装返回结果
                return new PageResult(orderVOPage.getTotal(), orderVOPage.getResult());
            }
        
        /**
         * 修改预约信息
         *
         * @param order 预约信息
         * @return int 操作成功条数
         */
        @Override
        public int update(Order order)
            {
                String orderStatus = order.getOrderStatus();
                if (!orderStatus.equals(Order.ORDERSTATUS_YES) && !orderStatus.equals(Order.ORDERSTATUS_NO) && !orderStatus.equals(Order.ORDERSTATUS_CANCEL))
                    {
                        //要修改的状态信息不符合预设信息，直接结束方法
                        return 0;
                    }
                //返回操作成功条数
                return orderMapper.update(order);
            }
    }
